﻿using System;
using WickedFlame.Core.Shapes;
using WickedFlame.Core.State;


namespace WickedFlame.Core.History
{
    public class ConvertToImageCommand : HistoryCommand
    {
        VectorShape shape;
        ImageShape image;
        ShapeMemento shapeMemento;
        ShapeMemento imageMemento;
        int shapeIndex;

        public ConvertToImageCommand(VectorShape shape, ImageShape image, int shapeIndex, int layerIndex)
            : base(layerIndex)
        {
            Title = "Convert to Image";

            this.shapeIndex = shapeIndex;
            this.shape = shape;
            if (shape is IStateCapturable)
                shapeMemento = ((IStateCapturable)shape).CreateMementoState();
            
            this.image = image;
        }

        /// <summary>
        /// sets the new state of the shapes in the selection list
        /// </summary>
        public void SetNewState()
        {
            imageMemento = ((IStateCapturable)image).CreateMementoState();
        }

        public override void Undo(ShapeList list)
        {
            if (shape is IStateCapturable)
                ((IStateCapturable)shape).ResetState(shapeMemento);

            list.Remove(image);
            list.Insert(shapeIndex, shape);
        }

        public override void Redo(ShapeList list)
        {
            ((IStateCapturable)image).ResetState(imageMemento);

            list.Remove(shape);
            list.Insert(shapeIndex, image);
        }
    }
}
